package com.ddwl.goods.controller.bargain;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.annotation.Limit;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.LimitType;
import com.ddwl.common.constant.LimitWay;
import com.ddwl.common.controller.BaseController;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.goods.service.bargain.BargainLogService;
import com.ddwl.goods.service.bargain.BargainService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.bargain.BargainLogBo;
import com.ddwl.schema.bo.goods.bargain.BargainLogPageBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.goods.bargain.BargainLogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author hemiaojun
 * @Date 2020/10/26 11:43
 */
@RestController
@RequestMapping("/bargainLog")
@Api(tags = "用户帮砍记录")
public class BargainLogController extends BaseController {

    @Resource
    private BargainLogService bargainLogService;
    @Resource
    private BargainService bargainService;

    @PostMapping("/helpBargain/{bargainOrderId}")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("帮好友砍价")
    @Limit(count = 5,limitWay = LimitWay.DB,limitType = LimitType.DK,errMsg = "当前设备砍价次数已达上线", period = 0)
    public RestVo<BargainLogVo> helpBargain(@PathVariable("bargainOrderId") String bargainOrderId){
        BargainLogBo bo = new BargainLogBo();
        bo.setBargainOrderId(bargainOrderId);
        bo.setUserId(getUserId());
        bo.setNickname(getUserNameNew());
        bo.setProduct(getProduct());
        return RestVo.ok(bargainService.helpBargain(bo,Boolean.FALSE));
    }

    @GetMapping("/check/{bargainOrderId}")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("检查是否能砍价")
    public RestVo<Boolean> check(@PathVariable("bargainOrderId") String bargainOrderId){
        return RestVo.ok(bargainService.check(bargainOrderId));
    }

    @PostMapping("/page")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("用户帮砍记录分页")
    public RestVo<PageVo<BargainLogVo>> page(@RequestBody @Valid PageBo<BargainLogPageBo> pageBo){
        return RestVo.ok(bargainLogService.page(pageBo));
    }

    @PostMapping("/share/page")
    @ApiOperation("分享页用户帮砍记录分页")
    public RestVo<PageVo<BargainLogVo>> sharePage(@RequestBody @Valid PageBo<BargainLogPageBo> pageBo){
        return RestVo.ok(bargainLogService.page(pageBo));
    }

    @GetMapping("/test/helpBargain/{bargainOrderId}")
    @ApiOperation("帮好友砍价test")
    @Auth(product = BaseEnum.ProductEnum.FENXIAO)
    public RestVo<List<BargainLogVo>> testHelpBargain(@PathVariable("bargainOrderId") String bargainOrderId, @RequestParam Integer bargainTimes, @RequestParam Boolean isNewUser) {
        List<BargainLogVo> list = new ArrayList<>();
        BargainLogBo bo = new BargainLogBo();
        bo.setBargainOrderId(bargainOrderId);
        bo.setProduct(getProduct());
        String randomStr = "_" + UUIDGenerator.getShortRandom() + "_";
        for (int i = 0; i < bargainTimes; i++) {
            bo.setUserId("Test" + randomStr + i);
            bo.setNickname("系统测试" + randomStr + i);
            list.add(bargainService.helpBargain(bo, isNewUser));
        }
        return RestVo.ok(list);
    }



}
